// pages/welfareMall/myPrize/index.js
//获取应用实例
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    //服务器图片的路径片段
    imgUrl:app.globalData.iconImgUrl,
    StatusBar: app.globalData.StatusBar,   //状态栏高度
    CustomBar: app.globalData.CustomBar,  //头部nav高度
    backIcon: true,   // 返回按钮是否显示
    title: "我的奖品",  //页面标题
    backImg: app.globalData.iconImgUrl+"arrow-left.svg",
    hasData: true,
    cartList: [],
    selCount: 0,
    totalPrice: 0,
    totalHonor: 0,
    pageSize: 8,
    currentPage: 1,
    shopcartList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getList()
  },
  getList() {
    // console.log('currentPage', this.data.currentPage)
    const data = {
      "userCode": wx.getStorageSync('userCode'),
      "currentPage": this.data.currentPage,
      "pageSize": this.data.pageSize
    }
    const cartList = this.data.cartList
    // console.log(data)
    const that = this
    wx.request({
      url: app.globalData.baseUrl + 'prizeorder/getPrizeList.do',
      method: 'post',
      data: data,
      success(res) {
        console.log('奖品 ', res)
        const pageInfo = res.data.pageInfo
        res.data.prizeList.forEach(item => {
          cartList.push(item)
        })
        that.setData({
          cartList: cartList,
          totalPage: pageInfo.totalResult / pageInfo.pageSize
        })
        return
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  handleChange({ detail }) {
    console.log(detail)
    var index = detail.e.target.dataset.itemindex,
      type = detail.type;
    var item = "cartList[" + index + "].number",
      totalPrice = this.data.totalPrice,
      price = parseInt(this.data.cartList[index].cash),
      selCount = this.data.selCount,
      checkAll = this.data.checkAll;
    // var price = "cartList[" + index + "].price",
    //       historyPrice = this.data.cartList[index].price;
    // var honor = "cartList[" + index + "].honor",
    //       historyHonor = this.data.cartList[index].honor;
    if (type == "plus" && checkAll) {
      this.setData({
        [item]: detail.value,
        totalPrice: totalPrice + price,
        selCount: selCount + 1
      })
    } else if (type == "minus" && checkAll) {
      this.setData({
        [item]: detail.value,
        totalPrice: totalPrice - price
      })
    }

  },
  seledItem(e) {
    // console.log(e.currentTarget.dataset.index)
    var index = e.currentTarget.dataset.index,
          id = e.currentTarget.dataset.id;
    var item = "cartList[" + index + "].seled";
    var seled = this.data.cartList[index].seled,
          number = this.data.cartList[index].num,
          cash = this.data.cartList[index].cash,
          honor = this.data.cartList[index].ydPrice,
          selCount = this.data.selCount,
          totalPrice = this.data.totalPrice,
          totalHonor = this.data.totalHonor,
          length = this.data.cartList.length,
          totalCount = 0,
          shopcartList = this.data.shopcartList;
    // 计算总数量
    for(var i = 0; i < length; i++) {
      totalCount += this.data.cartList[i].num
    }

    if (seled) {
      shopcartList.indexOf(id);
      this.setData({
        [item]: false,
        totalPrice: totalPrice - cash* number,
        totalHonor: totalHonor - honor * number,
        selCount: selCount - number,
        shopcartList: shopcartList
      })
    } else {
      shopcartList.push(id)
      this.setData({
        [item]: true,
        totalPrice: totalPrice + cash * number,
        totalHonor: totalHonor + honor * number,
        selCount: selCount + number,
        shopcartList: shopcartList
      })
      selCount = this.data.selCount;
      if (selCount === totalCount) {
       this.setData({
         checkAll: true
       })
      }
    }
  },
  selAll() {
    var that = this,
      length = this.data.cartList.length,
      checkAll = this.data.checkAll,
      checkVal,
      selCount = 0,
      totalPrice = 0,
      totalHonor = 0,
      shopcartList = [];

    if (checkAll) {
      checkVal = false
    } else {
      checkVal = true
      that.data.cartList.forEach(item => {
        console.log(item)
        shopcartList.push(item.prizeorderId)
      })
    }
    for (var i = 0; i < length; i++) {
      let item = "cartList[" + i + "].seled"
      if (!checkAll) {
        selCount += that.data.cartList[i].num
        // console.log(that.data.cartList[i].cash )
        totalPrice = that.data.cartList[i].cash * that.data.cartList[i].num
        totalHonor = that.data.cartList[i].ydPrice * that.data.cartList[i].num
      }
      that.setData({
        [item]: checkVal
      })
    }
    that.setData({
      checkAll: checkVal,
      selCount: selCount,
      totalPrice: totalPrice,
      totalHonor: totalHonor,
      shopcartList: shopcartList
    })
  },
  //去付款
  toPay() {
    var shopcartList = this.data.shopcartList
    if (shopcartList.length > 0) {
      wx.navigateTo({
        url: "order/index?idArr=" + shopcartList.join(',')
      })
    } else {
      wx.showToast({
        title: '请选择商品',
        image: '/assets/images/icon/error.png'
      })
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    const that = this
    const currentPage = that.data.currentPage
    const totalPage = that.data.totalPage
    if (currentPage < totalPage ) {
      // currentPage = that.data.currentPage + 1
      that.setData({
        currentPage: currentPage + 1
      })
      that.getList()
    }
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})